//
//  MainHomeSCKBViewCell.m
//  XTCloud
//
//  Created by QA on 16/8/23.
//  Copyright © 2016年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MainHomeSCKBViewCell.h"
#import "WebServiceCommonRequest.h"
#import "NSDictionary+DataFormat.h"
#import "AbstractBaseViewController.h"
#import "QueryMessageService.h"

#import "WebViewController.h"
#import "GEPushViewControllerHelper.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "GetUserStationService.h"
#import "JGProgressHUDHelper.h"

#define QueryCirculationTime 60.0

@interface MainHomeSCKBViewCell ()

@property (nonatomic, strong) UINavigationController *navigationController;
@property (nonatomic, strong) NSTimer *queryMessageTimer;

@property (nonatomic, strong) IBOutlet UIActivityIndicatorView *indicatorView;
@property (nonatomic, strong) NSArray *jrkbDataArray;

@property (nonatomic, strong) IBOutlet UILabel *fhflLabel;
@property (nonatomic, strong) IBOutlet UILabel *jhflLabel;
@property (nonatomic, strong) IBOutlet UILabel *rwdsLabel;
@property (nonatomic, strong) IBOutlet UILabel *fcccLabel;
@property (nonatomic, strong) IBOutlet UILabel *titleLabel;

@end

@implementation MainHomeSCKBViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self){
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"MainHomeSCKBViewCell" owner:self options:nil];
        self = [arrayOfViews objectAtIndex:0];
    }
    return self;
}

-(void)setNavigation:(UINavigationController*)navigation index:(NSInteger)index
{
    self.navigationController = navigation;
    self.indicatorView.hidden = YES;
    
    ModuleAuthorityEntity *authorityEntity = [self getModuleMenuEntityCode:@"SJXMYYGL_KSGN_JRKB"];
    if(!authorityEntity) return;
    
    //获取今日看板数据
    if(index == 0){
        self.titleLabel.text = @"今日看板";
        if(authorityEntity) self.queryMessageTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(getJRKBService) userInfo:nil repeats:YES];
    } else {
        self.titleLabel.text = @"昨日看板";
        if(authorityEntity) [self getZRKBService];//不需要定时获取
    }
    
    //添加点击事件
    if(index == 0){
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
        [self.contentView addGestureRecognizer:tap1];
    }
}

-(void)OnTapBtnView:(UITapGestureRecognizer *)sender
{
    NSLog(@"tag:%ld",sender.view.tag);
    
    ModuleAuthorityEntity *authorityEntity = [self getModuleMenuEntityCode:@"SJXMYYGL_KSGN_JRKB"];
    if(!authorityEntity){
        [JGProgressHUDHelper showErrorHUDText:@"无权限"];
        return;
    }
    
    if(authorityEntity.codeType.integerValue == 1){
        //网页模式
        WebPushParameterModel *model = [[WebPushParameterModel alloc] init];
        model.URL = authorityEntity.webCode;
        model.Title = authorityEntity.title;
        model.CodeType = CodeTypeWeb;
        
        WebViewController *viewController = [[WebViewController alloc] initWithPushParameterModel:model];
        [self.navigationController pushViewController:viewController animated:YES];
    } else {
        //原生APP模式
        [GEPushViewControllerHelper pushViewControllerHomePageCode:authorityEntity.nativeCode navigation:self.navigationController];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)dealloc
{
    if (self.queryMessageTimer){
        [self.queryMessageTimer setFireDate:[NSDate distantFuture]];
        [self.queryMessageTimer invalidate];
        self.queryMessageTimer = nil;
    }
}

//获取菜单权限信息
- (ModuleAuthorityEntity*)getModuleAuthorityEntity:(id)idValue
{
    NSPredicate *menuPredicat = [NSPredicate predicateWithFormat:@"id = %@", idValue];
    NSArray *menuArray = [ModuleAuthorityEntity MR_findAllSortedBy:@"order" ascending:YES withPredicate:menuPredicat];
    if(menuArray.count <=0){
        return nil;
    } else {
        return menuArray.firstObject;
    }
}

#pragma mark - 查询模块信息 ModuleMenuEntity

- (ModuleAuthorityEntity*)getModuleMenuEntityCode:(NSString*)code
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nativeCode = %@", code];
    ModuleAuthorityEntity *entity = [ModuleAuthorityEntity MR_findFirstWithPredicate:predicate sortedBy:@"order" ascending:YES];
    return entity;
}

#pragma mark - 今日看板数据处理

- (void)getJRKBService
{
    //开始加载进度
    self.indicatorView.hidden = NO;
    [self.indicatorView startAnimating];
    
    //暂停定时器
    [self.queryMessageTimer setFireDate:[NSDate distantFuture]];
    
    //获取网络数据
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"SCGL_SCJC_DATA"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if (result.resultArray && result.resultArray.count>0) {
            [self JRKBDataServiceHandleArray:result.resultArray.firstObject];
        }
        //开启定时器
        [self.queryMessageTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:QueryCirculationTime]];
        self.indicatorView.hidden = YES;
        [self.indicatorView stopAnimating];
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        //开启定时器
        [self.queryMessageTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:QueryCirculationTime]];
        self.indicatorView.hidden = YES;
        [self.indicatorView stopAnimating];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        //开启定时器
        [self.queryMessageTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:QueryCirculationTime]];
        self.indicatorView.hidden = YES;
        [self.indicatorView stopAnimating];
    }];
}

#pragma mark - 昨日看板数据处理

- (void)getZRKBService
{
    //开始加载进度
    self.indicatorView.hidden = NO;
    [self.indicatorView startAnimating];
    
    //获取网络数据
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"SCGL_SCJC_DATA_ZRKB"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if (result.resultArray && result.resultArray.count>0) {
            [self JRKBDataServiceHandleArray:result.resultArray.firstObject];
        }
        //开启定时器
        self.indicatorView.hidden = YES;
        [self.indicatorView stopAnimating];
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        //开启定时器
        self.indicatorView.hidden = YES;
        [self.indicatorView stopAnimating];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        //开启定时器
        self.indicatorView.hidden = YES;
        [self.indicatorView stopAnimating];
    }];
}

#pragma mark - 看板数据处理

- (void)JRKBDataServiceHandleArray:(NSArray*)array
{
    //数据赋值
    self.jrkbDataArray = array;
    
    CGFloat rwsl = 0;//任务数量
    CGFloat jhfl = 0;//计划方量
    CGFloat zjfl = 0;//计划方量
    CGFloat yfhl = 0;//已发货量
    CGFloat yfcc = 0;//已发车次
    CGFloat kpsl = 0;//开盘数量
    
    for(int i=0;i<array.count;i++){
        NSDictionary *mDictionary = [array objectAtIndex:i];
        rwsl += [mDictionary objectFormatFloatForKey:@"任务单数量"];
        jhfl += [mDictionary objectFormatFloatForKey:@"计划方量" retain:1 except:0];
        yfhl += [mDictionary objectFormatFloatForKey:@"发货方量" retain:1 except:0];
        zjfl += [mDictionary objectFormatFloatForKey:@"追加方量" retain:1 except:0];
        yfcc += [mDictionary objectFormatFloatForKey:@"车数"];
        kpsl += [mDictionary objectFormatFloatForKey:@"开盘数量"];
    }
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:13.0]};
    
    //设置开盘率
    NSString *rwslLabel = [NSString stringWithFormat:@"%0.0f",rwsl];
    NSString *jhflLabel = [NSString stringWithFormat:@"%0.1f",jhfl];
    NSString *yfhlLabel = [NSString stringWithFormat:@"%0.1f方",yfhl];
    NSString *yfccLabel = [NSString stringWithFormat:@"%0.0f",yfcc];
    //NSString *kplLabel = [NSString stringWithFormat:@"%0.1f%%开盘率", rwsl ==0 ? 0.0:kpsl/rwsl * 100];
    
    [self.jhflLabel setText:jhflLabel];
    [self.rwdsLabel setText:rwslLabel];
    [self.fcccLabel setText:yfccLabel];
    
    NSMutableAttributedString *yfhlLabelText = [[NSMutableAttributedString alloc] initWithString:yfhlLabel];
    [yfhlLabelText addAttributes:attributes range:NSMakeRange(yfhlLabelText.length - 1, 1)];
    [self.fhflLabel setAttributedText:yfhlLabelText];
    
}

@end

